package com.java.blog.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

/**
 * @author TaoYu
 * @Description 配置文件工具类
 */
public class PropertiesUtil {

	private static final Logger log = Logger.getLogger(PropertiesUtil.class);

	private Properties props;

	private static Map<String, Properties> propsCache = new ConcurrentHashMap<>();

	@SuppressWarnings("unused")
	private PropertiesUtil() {
	}

	public PropertiesUtil(String path) {
		InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(path);
		try {
			if (propsCache.containsKey(path)) {
				this.props = propsCache.get(path);
			} else {
				props = new Properties();
				props.load(inputStream);
				propsCache.put(path, props);
			}
		} catch (IOException e) {
			log.error("读取配置文件失败", e);
		}
	}

	public Boolean getBoolean(String key) {
		return Boolean.parseBoolean(props.getProperty(key));
	}

	public Integer getInteger(String key) {
		Integer value = null;
		try {
			value = Integer.parseInt(props.getProperty(key));
		} catch (Exception e) {
			log.error("转换参数失败", e);
		}
		return value;
	}

	public Long getLong(String key) {
		Long value = null;
		try {
			value = Long.parseLong(props.getProperty(key));
		} catch (Exception e) {
			log.error("转换参数失败", e);
		}
		return value;
	}

	public String getString(String key) {
		return props.getProperty(key);
	}

}
